# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： order_page.py
# @Time：2023/9/11 17:43
# @Author：lizhixiong
from time import sleep

import allure
from selenium.webdriver.common.by import By

from common.commin.commin import object_left_click, object_send_keys, allure_save_screenshot
from common.log.log import log
from data.names import OrderSel, PublicSel
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage


class OrderPage(JHBase):
    """
    订单菜单页面
    """

    @allure.step('搜索订单')
    def search_order(self, order_id):
        try:
            log.info('*****搜索订单*****')
            JHHTHomePage(self.driver).open_menu('yg', '订单')
            object_send_keys(self.driver, OrderSel.el_order_004(), order_id)
            object_left_click(self.driver, OrderSel.el_order_005())
            sleep(3)
        except Exception as e:
            log.error(f'搜索订单，异常为{e}')
            raise e

    @allure.step('查看订单详情页物流信息')
    def view_order_details_logistics(self, order_id):
        try:
            log.info('*****查看订单详情页物流信息*****')
            self.search_order(order_id)
            object_left_click(self.driver, OrderSel.el_order_006())  # 点击更多
            object_left_click(self.driver, OrderSel.el_order_007())  # 点击查看详情
            object_left_click(self.driver, OrderSel.el_order_011())  # 点击物流查询
            sleep(2)
            # allure_save_screenshot(self.driver, '物流查询窗口')
            object_left_click(self.driver, OrderSel.el_order_013())  # 点击关闭
            object_left_click(self.driver, OrderSel.el_order_014())  # 关闭详情页面
            return True
        except Exception as e:
            log.error(f'查看订单详情页物流信息，异常为{e}')
            raise e

    @allure.step('查看订单记录')
    def view_order_records(self, order_id):
        try:
            log.info('*****查看订单记录*****')
            for i in range(3):
                self.search_order(order_id)
                object_left_click(self.driver, OrderSel.el_order_006())  # 点击更多
                object_left_click(self.driver, OrderSel.el_order_008())  # 点击订单记录
                sel_text_1 = self.driver.find_element(By.XPATH, "//div[contains(text(),'订单生成')]").text
                sel_text_2 = self.driver.find_element(By.XPATH, "//div[contains(text(),'用户付款成功')]").text
                log.info(f'获取的text文案分别为：{sel_text_1, sel_text_2}')
                if '订单生成' == sel_text_1 and '用户付款成功' == sel_text_2:
                    return True
            return False
        except Exception as e:
            log.error(f'查看订单记录，异常为{e}')
            raise e

    @allure.step('订单备注')
    def order_notes(self, order_id):
        try:
            log.info('*****查看订单记录*****')
            self.search_order(order_id)
            object_left_click(self.driver, OrderSel.el_order_006())  # 点击更多
            object_left_click(self.driver, OrderSel.el_order_009())  # 点击订单备注
            object_send_keys(self.driver, OrderSel.el_order_009_01(), '测试备注')  # 输入备注
            object_left_click(self.driver, PublicSel.el_ok_button())  # 点击确定
        except Exception as e:
            log.error(f'查看订单记录，异常为{e}')
            raise e

    @allure.step('订单补单')
    def order_replenishment(self, order_id):
        try:
            log.info('*****订单补单*****')
            self.search_order(order_id)
            object_left_click(self.driver, OrderSel.el_order_006())  # 点击更多
            object_left_click(self.driver, OrderSel.el_order_010())  # 点击订单补单
            object_left_click(self.driver, OrderSel.el_order_015())  # 点击选择日期
            object_left_click(self.driver, OrderSel.el_order_016())  # 选择12号
            object_left_click(self.driver, OrderSel.el_order_017())  # 点击提交
            object_left_click(self.driver, OrderSel.el_order_018())  # 弹框二次确认
            sleep(2)
            # 补单成功后回到页面，获取元素文案（撤销补单）
            sel_text = self.driver.find_element(By.XPATH,
                                                '(//button[@class="el-button el-button--warning el-button--medium"])').text
            log.info(f'获取到的按钮元素文案为：{sel_text}')
            return sel_text
        except Exception as e:
            log.error(f'订单补单，异常为{e}')
            raise e

    @allure.step('撤销补单')
    def cancel_supplementary_order(self, order_id):
        try:
            log.info('*****撤销补单*****')
            self.search_order(order_id)
            object_left_click(self.driver, OrderSel.el_order_006())  # 点击更多
            object_left_click(self.driver, OrderSel.el_order_010())  # 点击订单补单
            object_left_click(self.driver, OrderSel.el_order_019())  # 点击撤销补单
            object_left_click(self.driver, PublicSel.el_ok_button())  # 二次确认弹框，点击确定
            # 取消补单后，按钮右“撤销补单”更新为：提交
            button = self.driver.find_elements(By.XPATH,
                                               '(//button[@class="el-button el-button--primary el-button--medium"])')
            sel_text = button[-1].text
            log.info(f'撤销补单后的按钮为：{sel_text}')
            return sel_text
        except Exception as e:
            log.error(f'撤销补单，异常为{e}')
            raise e
